package com.yanqu.road.dao.impl.activity.xingyoushanshui;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.youxing.XingYouShanShuiUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class XingYouShanShuiUserDataDaoImpl extends TempDataObjectDao<XingYouShanShuiUserData> {


    public static String getXingYouShanShuiUserDataSql = "select * from `t_u_xing_you_user_data` where `activityId`=? and `userId` = ?";
    public XingYouShanShuiUserData getXingYouShanShuiUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getXingYouShanShuiUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateXingYouShanShuiUserDataSql = "update `t_u_xing_you_user_data` set totalEnergy=?,totalSilverCoin=?,teamTotalEnergy=?,teamTotalSilverCoin=?,lastAbilityUpload=?,round=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(XingYouShanShuiUserData xingYouShanShuiUserData) {
        xingYouShanShuiUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, xingYouShanShuiUserData.getTotalEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, xingYouShanShuiUserData.getTotalSilverCoin()));
        para.put(i++, new DbParameter(Types.INTEGER, xingYouShanShuiUserData.getTeamTotalEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, xingYouShanShuiUserData.getTeamTotalSilverCoin()));
        para.put(i++, new DbParameter(Types.BIGINT, xingYouShanShuiUserData.getLastAbilityUpload()));
        para.put(i++, new DbParameter(Types.INTEGER, xingYouShanShuiUserData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, xingYouShanShuiUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, xingYouShanShuiUserData.getUserId()));
        if (getDb().executeNoneQuery(updateXingYouShanShuiUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertXingYouShanShuiUserDataSql = "insert into `t_u_xing_you_user_data`(activityId,userId,totalEnergy,totalSilverCoin,teamTotalEnergy,teamTotalSilverCoin,lastAbilityUpload,round) values (?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(XingYouShanShuiUserData xingYouShanShuiUserData) {
        xingYouShanShuiUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, xingYouShanShuiUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, xingYouShanShuiUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, xingYouShanShuiUserData.getTotalEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, xingYouShanShuiUserData.getTotalSilverCoin()));
        para.put(i++, new DbParameter(Types.INTEGER, xingYouShanShuiUserData.getTeamTotalEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, xingYouShanShuiUserData.getTeamTotalSilverCoin()));
        para.put(i++, new DbParameter(Types.BIGINT, xingYouShanShuiUserData.getLastAbilityUpload()));
        para.put(i++, new DbParameter(Types.INTEGER, xingYouShanShuiUserData.getRound()));
        if (getDb().executeNoneQuery(insertXingYouShanShuiUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected XingYouShanShuiUserData parseData(ResultSet rs) throws SQLException {
        XingYouShanShuiUserData xingYouShanShuiUserData = new XingYouShanShuiUserData();
        xingYouShanShuiUserData.setActivityId(rs.getInt("activityId"));
        xingYouShanShuiUserData.setUserId(rs.getLong("userId"));
        xingYouShanShuiUserData.setTotalEnergy(rs.getInt("totalEnergy"));
        xingYouShanShuiUserData.setTotalSilverCoin(rs.getLong("totalSilverCoin"));
        xingYouShanShuiUserData.setTeamTotalEnergy(rs.getInt("teamTotalEnergy"));
        xingYouShanShuiUserData.setTeamTotalSilverCoin(rs.getLong("teamTotalSilverCoin"));
        xingYouShanShuiUserData.setLastAbilityUpload(rs.getLong("lastAbilityUpload"));
        xingYouShanShuiUserData.setRound(rs.getInt("round"));

        return xingYouShanShuiUserData;
    }
}
